/****************************************************************************/
/* NAME:           light.c                                                  */
/* PURPOSE:        Osmolator management                                     */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  07/05/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "light.h"
#include "clock.h"
#include "Hal_OutputManagement.h"

#ifdef _LIGHT

#define Light_TASK_DELAY    (25000)

/**
  * @brief  Start a light out.
  * @param  None
  * @retval None
  */
#define start_light(light) (OUTPUT_SetOutputValue(light.OutputName, TRUE))

/**
  * @brief  Stop a light out.
  * @param  None
  * @retval None
  */
#define stop_light(light)  (OUTPUT_SetOutputValue(light.OutputName, FALSE))


static Light_def_t lights[] =
{
    { 0, 9,  0, 19,  0, 0, LIGHT_OUT_1},
    { 1, 9, 15, 19, 15, 0, LIGHT_OUT_2},
    { 2, 9, 30, 19, 30, 0, LIGHT_OUT_3},
    { 3, 9, 45, 19, 45, 0, LIGHT_OUT_4},
    { 4,10,  0, 20,  0, 0, LIGHT_OUT_5},
    { 5,10, 15, 20, 15, 0, LIGHT_OUT_6}
};

#define Light_NUMBER        (sizeof(lights) / sizeof(lights[0]))


/**
  * @brief  Main function of light module.
  * @param  none
  * @retval None
  */
void vTaskCTRL_ECLAIRAGE()
{
    portTickType  xLastWakeTime;
    Light_def_t   light_x;
    uint8_t       i;
    uint32_t      HH;
    uint32_t      MM;
    char          mes[32];

    /* init */
    xLastWakeTime = xTaskGetTickCount();
    Light_init();

    while(1)
    {
        /* check if an update is available */
        if ( uxQueueMessagesWaiting( light_queue ) > 0 )
        {
            xQueueReceive( light_queue, &light_x, 10);

            lights[light_x.id].time_start_HH = light_x.time_start_HH;
            lights[light_x.id].time_start_MM = light_x.time_start_MM;
            lights[light_x.id].time_stop_HH  = light_x.time_stop_HH;
            lights[light_x.id].time_stop_MM  = light_x.time_stop_MM;
            lights[light_x.id].light_auto    = light_x.light_auto;

            sprintf(mes, "màj eclairage %d", light_x.id+1);
            send_message(LIGHT_MODULE_ID, normal, mes);
        }

        HH = getHour();
        MM = getMin();

        /* if start is required */
        for (i = 0; i < Light_NUMBER; i++)
        {
            if (((lights[i].time_start_HH == HH) && (lights[i].time_start_MM == MM))
              || (lights[i].light_auto    != FALSE))
            {
                sprintf(mes, "start light %d", i);
                send_message(LIGHT_MODULE_ID, normal, mes);
                start_light(lights[i]);
            }
        }

        /* if stop is required */
        for (i = 0; i < Light_NUMBER; i++)
        {
            if (((lights[i].time_stop_HH == HH && lights[i].time_stop_MM == MM))
              || (lights[i].light_auto   == FALSE))
            {
                sprintf(mes, "stop light %d", i);
                send_message(LIGHT_MODULE_ID, normal, mes);
                stop_light(lights[i]);
            }
        }

        wait(Light_TASK_DELAY);
    } /* Never exit this loop */
}

/**
  * @brief  Initialisation of modules.
  * @param  none
  * @retval None
  */
void Light_init()
{

}

/**
  * @brief  Get the status of light choosen.
  * @param  light ID
  * @retval light status
  */
bool Light_GetLightStatus(uint8_t lightID)
{
    return OUTPUT_GetOutputValue(lights[lightID].OutputName);
}

#endif /* _ECLAIRAGE */
